package com.yy.jhl.ui.manager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.StatusBarCompat;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.app.MyApplication;
import com.yy.jhl.bean.LoginInfo;
import com.yy.jhl.interfaces.MessageNumCallBack;
import com.yy.jhl.interfaces.UserInfoCallBack;
import com.yy.jhl.ui.manager.contract.MenuThreeContract;
import com.yy.jhl.ui.manager.presenter.MenuThreePresenter;
import com.yy.jhl.ui.qrcode.QRScanActivity;
import com.yy.jhl.ui.user.activity.BindPhoneActivity;
import com.yy.jhl.ui.user.activity.CompanyInfoActivity;
import com.yy.jhl.ui.user.activity.LoginSelectActivity;
import com.yy.jhl.ui.user.activity.ModifyPwdActivity;
import com.yy.jhl.ui.user.activity.PersonInfoActivity;
import com.yy.jhl.ui.user.activity.ProvisionalCodeSendActivity;
import com.yy.jhl.util.DataRequestUtil;
import com.yy.jhl.util.DataUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/4.
 */

public class MenuThreeActivity extends BaseActivity<MenuThreePresenter> implements MenuThreeContract.View {
    @BindView(R.id.msg_text)
    TextView msgText;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_parent)
    LinearLayout titleParent;
    @BindView(R.id.scan_img)
    ImageView scanImg;
    @BindView(R.id.user_icon_img)
    ImageView userIconImg;
    @BindView(R.id.company_name)
    TextView companyName;
    @BindView(R.id.company_auth_img)
    ImageView companyAuthImg;
    @BindView(R.id.company_info)
    LinearLayout companyInfo;
    @BindView(R.id.modify_pwd)
    LinearLayout modifyPwd;
    @BindView(R.id.bind_phone)
    LinearLayout bindPhone;
    @BindView(R.id.set_msg)
    LinearLayout setMsg;
    @BindView(R.id.quit_login)
    LinearLayout quitLogin;
    @BindView(R.id.company_parent)
    LinearLayout companyParent;
    @BindView(R.id.person_name)
    TextView personName;
    @BindView(R.id.person_tel)
    TextView personTel;
    @BindView(R.id.person_parent)
    LinearLayout personParent;
    @BindView(R.id.person_info)
    LinearLayout personInfo;
    @BindView(R.id.send_code_parent)
    LinearLayout sendCodeParent;
    @BindView(R.id.about_us)
    LinearLayout aboutUsParent;
    @BindView(R.id.collect_parent)
    LinearLayout collectParent;
    @BindView(R.id.unread_image)
    ImageView unreadImage;
    private int loginType = 1;//1 :供应商 2：采购商
    @Override
    public int getLayoutId() {
        return R.layout.act_menu_three;
    }

    @Override
    public void initView() {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.deep_green_color));
        showData();
    }

    private void showData() {
        loginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        LoginInfo.UserBean userInfo = DataUtil.getUserInfo(mContext);
        LoginInfo.UserBean.CompanyBean company = userInfo.getCompany();
        if (loginType == 1){
            userIconImg.setImageResource(R.mipmap.gys_logo);
            companyParent.setVisibility(View.VISIBLE);
            collectParent.setVisibility(View.VISIBLE);
            companyInfo.setVisibility(View.VISIBLE);
            personParent.setVisibility(View.GONE);
            personInfo.setVisibility(View.GONE);
            sendCodeParent.setVisibility(View.GONE);
            companyName.setText(StringUtil.isEmpty(company.getCompany_name()) ? company.getTelephone() : company.getCompany_name());
            if ("3".equals(company.getStatus())) { //已认证
                companyAuthImg.setImageResource(R.mipmap.renzheng_yes);
            } else {
                companyAuthImg.setImageResource(R.mipmap.renzheng_no);
            }
            if (!StringUtil.isEmpty(company.getLogo())){
                ImageLoaderUtils.displayRound(mContext,userIconImg,company.getLogo(),R.mipmap.gys_logo);
            }
        }else {
            userIconImg.setImageResource(R.mipmap.cgs_logo);
            companyParent.setVisibility(View.GONE);
            collectParent.setVisibility(View.GONE);
            companyInfo.setVisibility(View.GONE);
            personParent.setVisibility(View.VISIBLE);
            personInfo.setVisibility(View.VISIBLE);
            sendCodeParent.setVisibility(View.GONE);
            personName.setText(StringUtil.isEmpty(userInfo.getNickname()) ? userInfo.getUsername() : userInfo.getNickname());
            personTel.setText(userInfo.getMobile_phone());
            if (!StringUtil.isEmpty(userInfo.getAvatar())){
                ImageLoaderUtils.displayRound(mContext,userIconImg,userInfo.getAvatar(),R.mipmap.cgs_logo);
            }
        }

        }

    @Override
    protected void onResume() {
        super.onResume();
        DataRequestUtil.getInstance(mContext).getUserInfo(new UserInfoCallBack() {
            @Override
            public void getCallBack(LoginInfo.UserBean userBean) {
                showData();
            }
        });
            DataRequestUtil.getInstance(mContext).getUnReadMessageNum(new MessageNumCallBack() {
                @Override
                public void getCallBack(int total) {
                    if (total == 0){
                        unreadImage.setVisibility(View.GONE);
                    }else {
                        unreadImage.setVisibility(View.VISIBLE);
                    }
                }
            });
    }

    @OnClick({R.id.company_info, R.id.modify_pwd, R.id.bind_phone, R.id.set_msg, R.id.quit_login,
            R.id.msg_text, R.id.scan_img, R.id.person_info, R.id.send_code_parent,R.id.about_us,R.id.collect_parent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.company_info:
                startActivity(CompanyInfoActivity.class);
                break;
            case R.id.modify_pwd:
                startActivity(ModifyPwdActivity.class);
                break;
            case R.id.bind_phone:
                startActivity(BindPhoneActivity.class);
                break;
            case R.id.set_msg:
                break;
            case R.id.quit_login:
                mPresenter.logout();
                break;
            case R.id.msg_text:
                startActivity(MessageListActivity.class);
                break;
            case R.id.scan_img:
                startActivityForResult(QRScanActivity.class, 1000);
                break;
            case R.id.person_info:
                startActivity(PersonInfoActivity.class);
                break;
            case R.id.send_code_parent:
                startActivity(ProvisionalCodeSendActivity.class);
                break;
            case R.id.about_us:
                startActivity(AboutUsActivity.class);
                break;
            case R.id.collect_parent:
                startActivity(CollectListActivity.class);
                break;
        }
    }

    @Override
    public void logoutSuccess() {
        MyApplication.getInstance().clearData();
        startActivity(LoginSelectActivity.class);
        finish();
    }

    @Override
    public void logoutFail() {

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            String result = data.getStringExtra(QRScanActivity.EXTRA_QRCODE_SUCCESS);
            switch (requestCode) {
                case 1000:
                    if (result.equals("***")){
                        showShortToast("扫描结果格式有误！");
                    }else {
                        InviteBoxInfoActivity.startAction(mContext,result);
                    }
                    break;
            }
        }
    }
}
